package com.boss.salary.controller;
import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.service.ICommonService;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.AgencyPersonRestrict;
import com.boss.salary.service.IAgencyPersonRestrictService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestSchema(schemaId = "AgencyPersonRestrictController")
@RestController
@RequestMapping(value = "/AgencyPersonRestrictController")
public class AgencyPersonRestrictController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(AgencyPersonRestrictController.class);

    @Autowired
    private IAgencyPersonRestrictService agencyPersonRestrictService;
    @Autowired
    private ICommonService iCommonService;
    /**
     * 新增
     *
     * @param agencyPersonRestrict
     * @return
     */
    @PostMapping("/saveAgencyPersonRestrict")
    public ApiResultDTO saveAgencyPersonRestrict(@RequestHeader("tokenid") String tokenid,
                                                 @RequestBody AgencyPersonRestrict agencyPersonRestrict) {
        try {
            UserDTO userDTO = getUser(tokenid);
            agencyPersonRestrict.setMofDivCode(userDTO.getProvince());
            agencyPersonRestrictService.insertAgencyPersonRestrict(agencyPersonRestrict);
            return ApiResultDTO.success(null, "保存成功");
        } catch (Exception e) {
            logger.error("新增 单位信息表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "新增 单位信息表发生异常");
        }
    }
    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteAgencyPersonRestrict/{id}")
    public ApiResultDTO deleteAgencyPersonRestrict(@RequestHeader("tokenid") String tokenid,
                                                   @PathVariable("id") String id) {
        try {
            UserDTO userDTO = getUser(tokenid);
            String mofDivCode = userDTO.getProvince();
            agencyPersonRestrictService.deleteAgencyPersonRestrict(id, mofDivCode);
            return ApiResultDTO.success(null, "删除成功");
        } catch (Exception e) {
            logger.error("删除 单位信息表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "删除 单位信息表发生异常");
        }
    }
    /**
     * 修改
     *
     * @param agencyPersonRestrict
     * @return
     */
    @PostMapping("/updateAgencyPersonRestrict")
    public ApiResultDTO updateAgencyPersonRestrict(@RequestHeader("tokenid") String tokenid,
                                                   @RequestBody AgencyPersonRestrict agencyPersonRestrict) {
        try {
            UserDTO userDTO = getUser(tokenid);
            agencyPersonRestrict.setMofDivCode(userDTO.getProvince());
            agencyPersonRestrictService.updateAgencyPersonRestrict(agencyPersonRestrict);
            return ApiResultDTO.success(null, "修改成功");
        } catch (Exception e) {
            logger.error("修改 单位信息表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "修改 单位信息表发生异常");
        }
    }
    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/queryAgencyPersonRestrict")
    public ApiResultDTO queryAgencyPersonRestrict(@RequestHeader("tokenid") String tokenid,
                                                  @RequestParam(value = "page") String page,
                                                  @RequestParam(value = "limit") String limit,
                                                  @RequestParam(value = "agencyId") String agencyId,
                                                  @RequestParam(value = "state") String state) {

        try {
            UserDTO userDTO = getUser(tokenid);
            List<String> childrenId = iCommonService.getAgencyChildrenId(userDTO, agencyId);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<Map<String, Object>> pageDTO = agencyPersonRestrictService.queryAgencyPersonRestrictPage(paginationDTO, userDTO.getProvince(), childrenId, state);
            return ApiResultDTO.success(pageDTO, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 根据id 判断 新增或修改
     *
     * @param agencyPersonRestrictList
     * @return
     */
    @PostMapping("/insertUpdateAgencyPersonRestrict")
    public ApiResultDTO insertUpdateAgencyPersonRestrict(@RequestHeader("tokenid") String tokenid,
                                                         @RequestBody List<AgencyPersonRestrict> agencyPersonRestrictList) {
        try {
            UserDTO userDTO = getUser(tokenid);
            agencyPersonRestrictService.insertUpdateAgencyPersonRestrict(agencyPersonRestrictList,userDTO);
            return ApiResultDTO.success(null, "保存成功");
        } catch (Exception e) {
            logger.error(" 单位信息表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "单位信息表发生异常");
        }
    }
    /**
     * 根据agencyid 查这个单位下的所有人数以及状态
     *
     * @param tokenid
     * @param agencyId
     * @return
     */
    @GetMapping("/queryAgencyId")
    public ApiResultDTO queryAgencyId(@RequestHeader("tokenid") String tokenid,
                                      @RequestParam(value = "agencyId") String agencyId) {
        try {
            UserDTO userDTO = getUser(tokenid);
            List<AgencyPersonRestrict> queryAgencyIdList = agencyPersonRestrictService.queryAgencyIdList(agencyId, userDTO);
            return ApiResultDTO.success(queryAgencyIdList, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }
    /**
     * 工资类型
     */
    @GetMapping("/agencyType")
    public ApiResultDTO agencyType(@RequestHeader("tokenid") String tokenid) {
        try {
            String province = getUser(tokenid).getProvince();
            List<AgencyPersonRestrict> agencyType = agencyPersonRestrictService.agencyType(province);
            return ApiResultDTO.success(agencyType, null);
        } catch (Exception e) {
            logger.error(" 单位信息表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }
    /**
     * 根据agencyid 查这个单位下的所有人数以及状态
     *
     * @param tokenid
     * @param agencyId
     * @return
     */
    @GetMapping("/currentUnit")
    public ApiResultDTO currentUnit(@RequestHeader("tokenid") String tokenid,
                                    @RequestParam(value = "agencyId") String agencyId) {

        try {
            UserDTO userDTO = getUser(tokenid);
            List<AgencyPersonRestrict> queryAgencyIdList = agencyPersonRestrictService.currentUnitList(agencyId, userDTO);
            return ApiResultDTO.success(queryAgencyIdList, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }
}
